package com.jsbj.tools;

import javax.servlet.http.HttpServletRequest;

/**
 * @PROJECT_NAME: dj_zhgd
 * @ClassName: GetUserIdAndDeptId
 * @Author: zhangheng
 * @DATE: 2020/5/20 10:42
 * @Version 1.0
 **/
public class RequestHeaderUtils {

    public static String getUserId(HttpServletRequest request){
        String userId = request.getHeader("userId");
//        Long userId = null;
//        if(user_id != null && !"".equals(user_id)){
//            userId = Long.valueOf(user_id);
//        }
        return userId;
    }

    public static String getTenantCode(HttpServletRequest request){
        String tenantCodes = request.getHeader("tenantCode");
        String tenantCode = null;
        if(tenantCodes != null && !"".equals(tenantCodes)){
            tenantCode = tenantCodes;
        }
        return tenantCode;
    }

    public static String getOrgCode(HttpServletRequest request){
        String orgCOde = request.getHeader("orgCOde");
        return orgCOde;
    }

    public static String getform(HttpServletRequest request){
        String forms = request.getHeader("form");
        String form = null;
        if(forms != null && !"".equals(forms)){
            form =forms;
        }
        return form;
    }

    public static String getfileType(HttpServletRequest request){
        String fileTypes = request.getHeader("fileType");
        String fileType = null;
        if(fileTypes != null && !"".equals(fileTypes)){
            fileType =fileTypes;
        }
        return fileType;
    }

    public static String getIp(HttpServletRequest request){
        String remoteAddr = request.getRemoteAddr();
        String forwarded = request.getHeader("X-Forwarded-For");
        String realIp = request.getHeader("X-Real-IP");

        String ipAdress = null;
        if (realIp == null) {
            if (forwarded == null) {
                ipAdress = remoteAddr;
            } else {
                ipAdress = remoteAddr + "/" + forwarded.split(",")[0];
            }
        } else {
            if (realIp.equals(forwarded)) {
                ipAdress = realIp;
            } else {
                if(forwarded != null){
                    forwarded = forwarded.split(",")[0];
                }
                ipAdress = realIp + "/" + forwarded;
            }
        }
        return ipAdress;
    }

}
